from flask import blueprints  , render_template , request , url_for , jsonify
import os
import uuid

UPLOAD_FOLDER = 'static/uploads'
bp = blueprints.Blueprint('tools',__name__ , url_prefix= '/tools' )

ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS


@bp.route('/picture', methods=['POST'])
def upload_images():
    if 'images' not in request.files:
        return jsonify({'error': 'No files uploaded'}), 400

    files = request.files.getlist('images')
    urls = []
    for file in files:
        if file and allowed_file(file.filename):
            # 生成唯一文件名防止冲突
            filename = f"{uuid.uuid4().hex}.{file.filename.rsplit('.', 1)[1].lower()}"
            save_path = os.path.join(UPLOAD_FOLDER, filename)
            file.save(save_path)
            # 生成可直接访问的URL
            url = url_for('static', filename=f'uploads/{filename}', _external=True)
            urls.append(url)

    return jsonify({'urls': urls}), 200